package com.master.equipment.netty.server;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;

/**
 * @author 洲影
 * @desc TODO
 * @date 2022/7/4
 */

public class DelimiterBasedMessageEncoder extends MessageToByteEncoder<String> {

	//特殊字符-分隔符
	private String delimiter;

	public DelimiterBasedMessageEncoder(String delimiter) {
		this.delimiter = delimiter;
	}

	@Override
	protected void encode(ChannelHandlerContext ctx, String msg, ByteBuf out)
			throws Exception {
		// 在响应的数据后面添加分隔符
		ctx.writeAndFlush(Unpooled.wrappedBuffer((msg + delimiter).getBytes()));
	}
}
